package com.lanf.generator;

import com.lanf.common.result.Result;
import com.lanf.log.annotation.Log;
import com.lanf.log.type.BusinessType;
import com.lanf.model.system.SysMenu;
import com.lanf.system.code.CodeVo;
import com.lanf.system.code.Generator;
import com.lanf.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @author tanlingfei
 * @version 1.0
 * @description TODO
 * @date 2024/5/29 21:54
 */
@RestController
@RequestMapping("/code/generator")
public class CodeController {
    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("/save")
    @Log(title = "代码生成", businessType = BusinessType.INSERT)
    public Result save(@RequestBody CodeVo codeVo) {
        codeVo.setConnectionURL(dataSourceProperties.getUrl());
        codeVo.setDriverName(dataSourceProperties.getDriverClassName());
        codeVo.setUsername(dataSourceProperties.getUsername());
        codeVo.setPassword(dataSourceProperties.getPassword());
        Generator.generatOne(codeVo);

        String table = codeVo.getTabName();
        SysMenu sysMenu = new SysMenu();
        System.out.println(codeVo);

        long totalMilliSeconds = System.currentTimeMillis();
        String uuid = totalMilliSeconds + UUID.randomUUID().toString().substring(0,6);
        sysMenu.setId(uuid);
        sysMenu.setParentId("2");
        sysMenu.setName(table);
        sysMenu.setType(1);
        sysMenu.setPath(table.toLowerCase());
        sysMenu.setComponent("pages/business/"+table.toLowerCase());
        sysMenu.setSortValue(1);
        sysMenu.setStatus(1);
        sysMenu.setCreateTime(new Date());
        sysMenu.setUpdateTime(new Date());
        sysMenu.setIsDeleted(0);
        sysMenu.setVersion(1L);
        sysMenuService.save(sysMenu);

        SysMenu sysMenuAdd = new SysMenu();
        sysMenuAdd.setId(System.currentTimeMillis()+UUID.randomUUID().toString().substring(0,6));
        sysMenuAdd.setParentId(uuid);
        sysMenuAdd.setName("add");
        sysMenuAdd.setType(2);
        sysMenuAdd.setPerms("bnt."+table.toLowerCase()+".add");
        sysMenuAdd.setSortValue(1);
        sysMenuAdd.setStatus(1);
        sysMenuAdd.setCreateTime(new Date());
        sysMenuAdd.setUpdateTime(new Date());
        sysMenuAdd.setIsDeleted(0);
        sysMenuAdd.setVersion(1L);
        sysMenuService.save(sysMenuAdd);

        SysMenu sysMenuEdit = new SysMenu();
        sysMenuEdit.setId(System.currentTimeMillis()+UUID.randomUUID().toString().substring(0,6));
        sysMenuEdit.setParentId(uuid);
        sysMenuEdit.setName("update");
        sysMenuEdit.setType(2);
        sysMenuEdit.setPerms("bnt."+table.toLowerCase()+".update");
        sysMenuEdit.setSortValue(1);
        sysMenuEdit.setStatus(1);
        sysMenuEdit.setCreateTime(new Date());
        sysMenuEdit.setUpdateTime(new Date());
        sysMenuEdit.setIsDeleted(0);
        sysMenuEdit.setVersion(1L);
        sysMenuService.save(sysMenuEdit);

        SysMenu sysMenuRemove = new SysMenu();
        sysMenuRemove.setId(System.currentTimeMillis()+UUID.randomUUID().toString().substring(0,6));
        sysMenuRemove.setParentId(uuid);
        sysMenuRemove.setName("remove");
        sysMenuRemove.setType(2);
        sysMenuRemove.setPerms("bnt."+table.toLowerCase()+".remove");
        sysMenuRemove.setSortValue(1);
        sysMenuRemove.setStatus(1);
        sysMenuRemove.setCreateTime(new Date());
        sysMenuRemove.setUpdateTime(new Date());
        sysMenuRemove.setIsDeleted(0);
        sysMenuRemove.setVersion(1L);
        sysMenuService.save(sysMenuRemove);


        SysMenu sysMenuList = new SysMenu();
        sysMenuList.setId(System.currentTimeMillis()+UUID.randomUUID().toString().substring(0,6));
        sysMenuList.setParentId(uuid);
        sysMenuList.setName("list");
        sysMenuList.setType(2);
        sysMenuList.setPerms("bnt."+table.toLowerCase()+".list");
        sysMenuList.setSortValue(1);
        sysMenuList.setStatus(1);
        sysMenuList.setCreateTime(new Date());
        sysMenuList.setUpdateTime(new Date());
        sysMenuList.setIsDeleted(0);
        sysMenuList.setVersion(1L);
        sysMenuService.save(sysMenuList);

        return Result.ok();
    }
}
